<%@page import="java.util.ArrayList"%>
<%@page import="cn.sendto.model.ScoreEntity"%>
<%@page import="java.util.List"%>
<%@page import="cn.sendto.mapper.ScoreMapper"%>
<%@page import="cn.sendto.mapper.IRowMapper"%>
<%@page import="cn.sendto.dbhelper.DBHelper"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
   if(request.getSession().getAttribute("user")==null){
	   request.getRequestDispatcher("login.jsp").forward(request, response);
   }
    %>
<%
//标签名字符串
String str4=">";
String str1="<ul class='score_info_ul'><li";
String str2="</li><li";
String str3="</li></ul>";
String query="";

//几种情况下的SQL语句
final String QUERY_BASE="SELECT scoreinfo.*,studentinfo.studentname,courseinfo.coursename "
		   +"FROM scoreinfo,studentinfo,courseinfo "
		   +"WHERE scoreinfo.studentid=studentinfo.studentid ";

//查询所有人
final String QUERY_ALL=
           QUERY_BASE+"AND scoreinfo.courseid=courseinfo.courseid";
//根据id查询
final String QUERY_ID=
           QUERY_BASE+"AND scoreinfo.courseid=courseinfo.courseid AND scoreinfo.studentid=?";
//根据学生姓名查询
final String QUERY_STUDENT=
           QUERY_BASE+"AND scoreinfo.courseid=courseinfo.courseid AND studentinfo.studentname=?";
//根据课程查询
final String QUERY_COURSE=
           QUERY_BASE+"AND scoreinfo.courseid=courseinfo.courseid AND courseinfo.coursename=?";

//实例化
List<ScoreEntity> list;
DBHelper helper=new DBHelper();
IRowMapper mapper=new ScoreMapper();

request.setCharacterEncoding("utf-8");

//若通过学生信息列表进入，则获取id查询。
String id=request.getParameter("id");

//若通过搜索框进入，则获取学生姓名或课程姓名
String name=request.getParameter("name");

//单选按钮 ，判断搜索学生姓名还是课程姓名
String condition=request.getParameter("condition");

//查询所有人的成绩
if(id==null && name==null){
	   query=QUERY_ALL;
	   list=helper.executeQuery(query, mapper, null);
}else if(id!=null && name==null){
//查询学生id的成绩
	   query=QUERY_ID;
	   list=helper.executeQuery(query, mapper, id);
} else if(id==null && name!=null){
//同搜索框查询成绩
//通过判断单选按钮值来判断搜索方式
//查询学生姓名成绩
	if(condition.equals("1")){
		query=QUERY_STUDENT;
		list=helper.executeQuery(query, mapper, name);
	} else{
		
//查询课程名成绩
		query=QUERY_COURSE;
		list=helper.executeQuery(query, mapper, name);
	}
	   
} else{
	   list=new ArrayList<ScoreEntity>();
}


%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/main.css" />
<style type="text/css">
.aaa{
	
	margin-right: 20%;
}
.aaa input{
  height: 35px;
  width: 15%;
  font-size: 25px;
  color: #DC143C;
}
.bbb{
  margin-top:10px;
}
.bbb input{
  height: 20px;
  width: 20px;
  font-size: 20px;
  color: #DC143C;
}


</style>
</head>
<body class="class_info">
<div style="width: 100%; text-align: right;  margin-top:40px;" >
<form action="score_info.jsp" method="post" class="aaa">
<input type="text" name="name" value="<%out.print(name!=null?name:"");%>">
<input style="width:100px;"type="submit" value="搜索"/>
<div class="bbb">
<input type="radio" name="condition" value="1" 
<%if(condition==null || condition.equals("1")){out.print(" checked=\"checked\"");} %>
/>姓名
<input type="radio" name="condition" value="2" 
<%if(condition!=null && condition.equals("2")){out.print(" checked=\"checked\"");} %>
/>课程
</div>
</form>
</div>
	<div class="class_info_div">
		<ul class="score_info_ul">
			<li>序号</li>
			<li>姓名</li>
			<li>课程</li>
			<li>成绩</li>
			<li>修改</li>
			<li>删除</li>
		</ul>
	<% 
//遍历显示信息
	   for(int i=0;i<list.size();i++){
		   String qurey=str1+str4+(i+1)
					  +str2+str4+list.get(i).getStudentName()
					  +str2+str4+list.get(i).getCourseName()
					  +str2+str4+list.get(i).getScore()
					  +str2+str4+"<a href=\"modifi_score.jsp?id="+list.get(i).getScoreId()+""+"\">修改</a>"
					  +str2+str4+"<a href=\"delete.jsp?id="+list.get(i).getScoreId()+"\" onclick=\"return aaa()\" >删除</a>"
					  +str3;
			  out.print(qurey);
		  }
	  helper.close();
	%>
	</div>
</body>
</html>